package com.elm.dao;

import com.elm.entity.DeliveryAddress;
import com.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDao {
    public int doSaveAddress(DeliveryAddress deliveryAddress) throws SQLException {
        String sql = "INSERT INTO deliveryaddress(contactName, contactSex, contactTel, address, userId) VALUES(?,?,?,?,?)";

        try(Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, deliveryAddress.getContactName());
            preparedStatement.setInt(2, deliveryAddress.getContactSex());
            preparedStatement.setString(3, deliveryAddress.getContactTel());
            preparedStatement.setString(4, deliveryAddress.getAddress());
            preparedStatement.setString(5, deliveryAddress.getUserId());

            return preparedStatement.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int doUpdateAddress(DeliveryAddress deliveryAddress) throws SQLException {
        String sql = "UPDATE deliveryaddress SET contactName = ?, contactSex = ?, contactTel = ?, address = ? WHERE daId = ?";

        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, deliveryAddress.getContactName());
            ps.setInt(2, deliveryAddress.getContactSex());
            ps.setString(3, deliveryAddress.getContactTel());
            ps.setString(4, deliveryAddress.getAddress());
            ps.setInt(5, deliveryAddress.getDaId());
            return ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int doDeleteAddress(int daId) {
        String sql = "DELETE FROM deliveryaddress WHERE daId = ?";
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, daId);
            return ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public DeliveryAddress doGetAddressById(int daId) {
        String sql = "SELECT * FROM deliveryaddress WHERE daId = ?";
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, daId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                packageDeliveryAddress(rs, deliveryAddress);
            }
            return deliveryAddress;
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<DeliveryAddress> doGetAddressList(String userId) {
        String sql = "SELECT * FROM deliveryaddress WHERE userId = ?";
        List<DeliveryAddress> deliveryAddressList = new ArrayList<DeliveryAddress>();

        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                DeliveryAddress deliveryAddress = new DeliveryAddress();
                packageDeliveryAddress(rs, deliveryAddress);
                deliveryAddressList.add(deliveryAddress);
            }
            return deliveryAddressList;
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    private void packageDeliveryAddress(ResultSet rs, DeliveryAddress deliveryAddress) throws SQLException {
        deliveryAddress.setDaId(rs.getInt("daId"));
        deliveryAddress.setContactName(rs.getString("contactName"));
        deliveryAddress.setContactTel(rs.getString("contactTel"));
        deliveryAddress.setAddress(rs.getString("address"));
        deliveryAddress.setContactSex(rs.getInt("contactSex"));
        deliveryAddress.setDaId(rs.getInt("daId"));
        deliveryAddress.setUserId(rs.getString("userId"));
    }
}
